package com.hyl.config;

import jakarta.servlet.http.HttpSession;
import jakarta.websocket.HandshakeResponse;
import jakarta.websocket.server.HandshakeRequest;
import jakarta.websocket.server.ServerEndpointConfig;

/**
 * HTTP握手阶段(还未升级为 WebSocket时)保存 HttpSession 的引用
 * ChatEndpoint就通过这个配置类获取HttpSession对象进行操作（@ServerEndpoint中引入）
 */
public class GetHttpSessionConfig extends ServerEndpointConfig.Configurator{
    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        //获取HttpSession对象
        HttpSession httpSession = (HttpSession) request.getHttpSession();
        //保存HttpSession对象到ServerEndpointConfig对象中
        //ChatEndpoint类中在onOpen方法中通过EndpointConfig对象获取
        sec.getUserProperties().put(HttpSession.class.getName(), httpSession);
    }
}
